package zsc.ruanc.practicaltraining.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zsc.ruanc.practicaltraining.exception.ApiException;
import zsc.ruanc.practicaltraining.mapper.ArticleMapper;
import zsc.ruanc.practicaltraining.mapper.ClassificationMapper;
import zsc.ruanc.practicaltraining.model.domain.TClassification;
import zsc.ruanc.practicaltraining.service.ClassificationService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章分类
 */

@Service
public class ClassificationServiceImpl implements ClassificationService {

    @Autowired
    private ClassificationMapper classificationMapper;

    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 插入分类名
     * @param className
     * @return
     */
    @Override
    public boolean insert(String className) {
        if (classificationMapper.checkName(className)) {
            throw new ApiException(-1, "分类名已存在");
        }

        classificationMapper.insert(className, new Date());
        return true;
    }

    /**
     * 更新分类名
     * @param id
     * @param className
     * @return
     */
    @Override
    public boolean update(Integer id, String className) {
        if (classificationMapper.checkName(className)) {
            throw new ApiException(-1, "分类名已存在");
        }
        int x = classificationMapper.update(id, className);
        if (x == 0) {
            throw new ApiException(-1, "未找到匹配");
        }
        return true;
    }

    /**
     *  删除分类名
     * @param className
     * @return
     */
    @Override
    @Transactional
    public boolean deleteByName(String className) {
        if (className.equals("默认分类")) {
            throw new ApiException(-1, "默认分类不能删除");
        }
        if (!classificationMapper.checkName(className)) {
            throw new ApiException(-1, "没有这个分类");
        }
        int count = articleMapper.updateCategories(className, "默认分类");

        classificationMapper.numAnd("默认分类", count); // 把数量加到分类
        classificationMapper.deleteByName(className);

        return true;

    }

    /**
     * 获得分类名
     * @return
     */
    @Override
    public String[] getNameList() {
        return classificationMapper.getNameList();
    }

    /**
     * 获得全部数据
     * @return
     */
    @Override
    public Map selectPage(int cur, int size) {
        PageHelper.startPage(cur, size);
        PageInfo<TClassification> info = new PageInfo<>(this.classificationMapper.selectAll());
        Map map = new HashMap();
        map.put("total", info.getTotal());
        map.put("list", info.getList());
        return map;
    }

    /**
     * 按分类名查找
     * @param name
     * @return
     */
    public TClassification selectByName(String name) {
        return classificationMapper.selectByName(name);
    }



}
